package com.version2_0.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.util.aliyun.OCSutils;
import com.util.validate.UserValidate;
import com.version1_1.service.PageService;
import com.version2_0.service.PageService2_0;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * Created by reeveslee on 16/6/7.
 */
@Controller
public class PageController2_0 {
    private static final Logger LOGGER = Logger.getLogger(PageController2_0.class);

    @Autowired
    private PageService2_0 pageService20;

    @Autowired
    private PageService pageService;

    /**
     * 新版资讯请求接口
     *
     * @param sectionId
     * @param timestamp
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/v2.0/pages/{userId}/{sectionId}/{timestamp}/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String pages(@PathVariable("userId") int userId, @PathVariable("sectionId") int sectionId, @PathVariable("timestamp") long timestamp, @PathVariable("lastId") int lastId) {

        return pageService20.pages(userId, sectionId, timestamp, lastId);
    }

    /**
     * 文章内容接口
     *
     * @param pageId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/content/{pageId}", method = RequestMethod.GET, produces = "text/html;charset=utf-8")
    @ResponseBody
    public String page(@PathVariable("pageId") int pageId) {
        pageService.doRead(pageId);

        OCSutils ocSutils = new OCSutils();
        if (ocSutils.get("pageInfo" + pageId) == null) {
            ocSutils.save("pageInfo" + pageId, pageService20.content(pageId), 600);
            return pageService20.content(pageId);
        } else {
            return (String) ocSutils.get("pageInfo" + pageId);
        }
    }

    /**
     * 文章的其他信息
     *
     * @param pageId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/info/{pageId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String info(@RequestBody JSONObject jsonObject, @PathVariable("pageId") int pageId) {
        int userId = -1;
        if (jsonObject == null) {
            LOGGER.error("no token information received.");
        } else {
            userId = jsonObject.getIntValue("id");
        }
        // {"code":401,"info":"please log in first."}
        return pageService20.info(pageId, userId);
    }

    /**
     * 文章评论内容
     *
     * @param jsonObject
     * @param pageId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/comment/{pageId}/{lastId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String comment(@RequestBody JSONObject jsonObject, @PathVariable("pageId") int pageId, @PathVariable("lastId") int lastId) {
        int userId = -1;
        if (jsonObject == null) {
            LOGGER.error("no token information received.");
        } else {
            userId = jsonObject.getIntValue("id");
        }
        // {"code":401,"info":"please log in first."}
        return pageService20.comment(pageId, userId, lastId);
    }


    /**
     * 添加文章评论
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/v2.0/page/comment/add", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String addComment(@RequestBody JSONObject jsonObject) {
        // {"images":[],"text":"","userId":,"kbToken":"","pageId":}
        try {
            int userId = jsonObject.getIntValue("userId");
            String kbToken = jsonObject.getString("kbToken");

            if (UserValidate.validate(userId, kbToken) == 0) {
                // 没有token
                return "{\"code\":400,\"info\":\"please apply for token first.\"}";
            } else if (UserValidate.validate(userId, kbToken) == 2) {
                // token错误
                return "{\"code\":400,\"info\":\"token is not correct,please check it out.\"}";
            } else {
                JSONArray images = jsonObject.getJSONArray("images");
                String text = jsonObject.getString("text");
                int pageId = jsonObject.getIntValue("pageId");
                return pageService20.addComment(userId, pageId, text, images);
            }
        } catch (Exception e) {
            return "{\"code\":400,\"info\":\"error occurred\"}";
        }
    }

    /**
     * 删除文章评论
     *
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/v2.0/page/comment/delete", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String delComment(@RequestBody JSONObject jsonObject) {
        // {"userId":,"kbToken":"","commentId":}
        try {
            int userId = jsonObject.getIntValue("userId");
            String kbToken = jsonObject.getString("kbToken");

            if (UserValidate.validate(userId, kbToken) == 0) {
                // 没有token
                return "{\"code\":400,\"info\":\"please apply for token first.\"}";
            } else if (UserValidate.validate(userId, kbToken) == 2) {
                // token错误
                return "{\"code\":400,\"info\":\"token is not correct,please check it out.\"}";
            } else {
                int commentId = jsonObject.getIntValue("id");
                return pageService20.delComment(userId, commentId);
            }

        } catch (Exception e) {
            return "{\"code\":400,\"info\":\"error occurred\"}";
        }
    }

    /**
     * 用户投票
     *
     * @param jsonObject
     * @param chose
     * @return
     */
    @RequestMapping(value = "/v2.0/page/vote/chose/{id}/{chose}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String voteChose(@RequestBody JSONObject jsonObject, @PathVariable("id") int voteInfoId, @PathVariable("chose") int chose) {
        // chose是0的时候代表one,1代表second
        // {"userId":,"kbToken":""}
        try {
            int userId = jsonObject.getIntValue("userId");
            String kbToken = jsonObject.getString("kbToken");

            if (UserValidate.validate(userId, kbToken) == 0) {
                // 没有token
                return "{\"code\":400,\"info\":\"please apply for token first.\"}";
            } else if (UserValidate.validate(userId, kbToken) == 2) {
                // token错误
                return "{\"code\":400,\"info\":\"token is not correct,please check it out.\"}";
            } else {
                return pageService20.voteChose(userId, voteInfoId, chose);
            }

        } catch (Exception e) {
            LOGGER.error(e);
            return "{\"code\":400,\"info\":\"error occurred\"}";
        }
    }


    /**
     * 历史上的今天界面
     *
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/history/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String history(@PathVariable("lastId") int lastId) {
        return pageService20.history(lastId);
    }

    /**
     * video界面
     *
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/video/{lastId}", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String video(@PathVariable("lastId") int lastId) {
        return pageService20.video(lastId);
    }

    /**
     * vote界面
     *
     * @param lastId
     * @return
     */
    @RequestMapping(value = "/v2.0/page/vote/{lastId}", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    @ResponseBody
    public String vote(@RequestBody JSONObject jsonObject, @PathVariable("lastId") int lastId) {

        try {
            int userId = jsonObject.getLong("userId").intValue();
            String kbToken = jsonObject.getString("kbToken");

            if (UserValidate.validate(userId, kbToken) == 0) {
                // 没有token
                return "{\"code\":400,\"info\":\"please apply for token first.\"}";
            } else if (UserValidate.validate(userId, kbToken) == 2) {
                // token错误
                return "{\"code\":400,\"info\":\"token is not correct,please check it out.\"}";
            } else {
                return pageService20.vote(userId, lastId);
            }

        } catch (Exception e) {
            LOGGER.error(e);
            return "{\"code\":400,\"info\":\"error occurred\"}";
        }
    }


}
